"""
Case Type   : checksum
Case Name   : 表的字符编码格式不同，执行checksum语句
Create At   : 2024.09.23
Owner       : @zhanghuan96
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、创建opradmin用户
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_user_0004'
        self.u_name = 'u_user_0004'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = (f"drop database if exists {self.db_name};"
                   f"create database {self.db_name} dbcompatibility ='B';")
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(1,
                         sql_res.count(self.constant.CREATE_DATABASE_SUCCESS),
                         '兼容B库创建失败')
        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:创建opradmin用户;expect:成功-----'
        self.log.info(text)
        sql_cmd2 = (f"drop user if exists {self.u_name};"
                    f"create user if not exists  {self.u_name} opradmin "
                    f"password '{macro.COMMON_PASSWD}';"
                    f"select * from pg_user where usename= '{self.u_name}';")
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(sql_res2)
        res_checksum1 = sql_res2.splitlines()[-2].split('|')[-1].strip()
        self.log.info(res_checksum1)
        self.assertIn('CREATE ROLE', sql_res2, '创建失败')
        self.assertIn('(1 row)', sql_res2, '创建失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd3 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3, dbname='postgres')
        self.log.info(sql_res3)
        sql_cmd4 = f"drop user if exists {self.u_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertEqual(1,
                         sql_res3.count(self.constant.DROP_DATABASE_SUCCESS),
                         '清理环境失败')
        self.assertIn('DROP ROLE', sql_res4)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
